{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find Xor-Beauty of Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation #array #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算 #数组 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: xorBeauty"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #查询数组 Xor 美丽值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;。</p>\n",
    "\n",
    "<p>三个下标&nbsp;<code>i</code>&nbsp;，<code>j</code>&nbsp;和&nbsp;<code>k</code>&nbsp;的 <strong>有效值</strong>&nbsp;定义为&nbsp;<code>((nums[i] | nums[j]) &amp; nums[k])</code>&nbsp;。</p>\n",
    "\n",
    "<p>一个数组的 <strong>xor 美丽值</strong>&nbsp;是数组中所有满足&nbsp;<code>0 &lt;= i, j, k &lt; n</code>&nbsp;&nbsp;<strong>的三元组</strong>&nbsp;<code>(i, j, k)</code>&nbsp;的 <strong>有效值</strong>&nbsp;的异或结果。</p>\n",
    "\n",
    "<p>请你返回&nbsp;<code>nums</code>&nbsp;的 xor 美丽值。</p>\n",
    "\n",
    "<p><b>注意：</b></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>val1 | val2</code>&nbsp;是&nbsp;<code>val1</code> 和&nbsp;<code>val2</code>&nbsp;的按位或。</li>\n",
    "\t<li><code>val1 &amp; val2</code>&nbsp;是&nbsp;<code>val1</code> 和&nbsp;<code>val2</code>&nbsp;的按位与。</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>nums = [1,4]\n",
    "<b>输出：</b>5\n",
    "<b>解释：</b>\n",
    "三元组和它们对应的有效值如下：\n",
    "- (0,0,0) 有效值为 ((1 | 1) &amp; 1) = 1\n",
    "- (0,0,1) 有效值为 ((1 | 1) &amp; 4) = 0\n",
    "- (0,1,0) 有效值为 ((1 | 4) &amp; 1) = 1\n",
    "- (0,1,1) 有效值为 ((1 | 4) &amp; 4) = 4\n",
    "- (1,0,0) 有效值为 ((4 | 1) &amp; 1) = 1\n",
    "- (1,0,1) 有效值为 ((4 | 1) &amp; 4) = 4\n",
    "- (1,1,0) 有效值为 ((4 | 4) &amp; 1) = 0\n",
    "- (1,1,1) 有效值为 ((4 | 4) &amp; 4) = 4 \n",
    "数组的 xor 美丽值为所有有效值的按位异或 1 ^ 0 ^ 1 ^ 4 ^ 1 ^ 4 ^ 0 ^ 4 = 5 。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>nums = [15,45,20,2,34,35,5,44,32,30]\n",
    "<b>输出：</b>34\n",
    "<code><span style=\"\"><b>解释：</b>数组的 xor 美丽值为</span> 34 。</code>\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length&nbsp;&lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-xor-beauty-of-array](https://leetcode.cn/problems/find-xor-beauty-of-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-xor-beauty-of-array](https://leetcode.cn/problems/find-xor-beauty-of-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,4]', '[15,45,20,2,34,35,5,44,32,30]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        res = nums[0]\n",
    "        for i in range(1,len(nums)):\n",
    "            res ^= nums[i]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        ans = 0 \n",
    "        for i in nums:\n",
    "            ans = ans ^i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        xor = 0\n",
    "        for num in nums:\n",
    "            xor = xor ^ num \n",
    "        return xor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in nums:\n",
    "            ans ^= i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "         return reduce(xor, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for item in nums:\n",
    "            ans ^= item\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor,nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        for i in nums:\n",
    "            res ^= i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor, nums)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        x = 0\n",
    "        for y in nums:\n",
    "            x ^= y\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(lambda x, y: x^ y, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        xor = 0\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            xor = xor ^ nums[i]\n",
    "                # value_useful = nums[i]&nums[k]\n",
    "                # xor ^=  value_useful\n",
    "        return xor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in nums:\n",
    "            ans ^= i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor,nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return  reduce(lambda  x, y:x^y,nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor,nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        xor = 0\n",
    "        for num in nums:\n",
    "            xor = xor ^ num \n",
    "        return xor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "         return reduce(xor, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in nums:\n",
    "            ans ^= i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor,nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        res=0\n",
    "        for num in nums:\n",
    "            res ^= num\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        a=0\n",
    "        for i in nums:\n",
    "            a^=i\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for nu in nums:\n",
    "            ans ^= nu\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor, nums)\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in nums:\n",
    "            ans ^= i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor,nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor,nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for x in nums:\n",
    "            ans ^= x\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(lambda x,y:x^y, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in nums:\n",
    "            ans ^= i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        \n",
    "\n",
    "        return reduce(lambda x, y:x ^ y, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in nums:\n",
    "            ans ^= i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor,nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for num in nums:\n",
    "            ans = ans ^ num\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, a: List[int]) -> int:\n",
    "        ans=0\n",
    "        for i in a:\n",
    "            ans^=i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        return reduce(xor, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def xorBeauty(self, nums: List[int]) -> int:\n",
    "        res = 0  # 初始化结果为 0\n",
    "        for num in nums:  # 遍历整个数组\n",
    "            res ^= num  # 对每个元素执行异或操作\n",
    "        return res  # 返回结果\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
